import java.util.ArrayList;
import java.util.List;

/**
 * @author tmh
 * @date 2024/9/26 22:42
 * @description
 */
public class T46全排列 {
    private List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        dfs(nums, new boolean[nums.length], 0, new ArrayList<>());
        return result;
    }

    private void dfs(int[] nums, boolean[] used, int depth, List<Integer> list) {
        if (depth == nums.length) {
            result.add(new ArrayList<>(list));
        }

        for (int i = 0; i < nums.length; i++) {
            if (!used[i]) {
                used[i] = true;
                list.add(nums[i]);
                dfs(nums, used, depth + 1, list);
                used[i] = false;
                list.remove(list.size() - 1);
            }
        }
    }
}
